define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'card/type/index' + location.search,
                    add_url: 'card/type/add',
                    //edit_url: 'card/type/edit',
                    //del_url: 'card/type/del',
                    multi_url: 'card/type/multi',
                    import_url: 'card/type/import',
                    table: 'card_type',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                escape:false,
                commonSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        //{field: 'pid', title: __('Pid')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'switch', title: __('Switch'), searchList: {"0":__('Switch 0'),"1":__('Switch 1')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'add_subclass',
                                    title: __('添加子类'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '添加子类',
                                    //icon: 'fa fa-folder-o',
                                    url: function(row,column){
                                        var url='card/type/add?pid='+row.id;
                                        return url;
                                    },/*visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.pid>0 ) {
                                            return false
                                        }
                                        return true;

                                    },*/
                                },
                                {
                                    name: 'click',
                                    title: __('点击执行事件'),
                                    classname: 'btn btn-xs btn-success btn-click',
                                    icon: 'fa fa-pencil',
                                    // dropdown: '更多',//如果包含dropdown，将会以下拉列表的形式展示
                                    click: function (data) {
                                        Layer.alert("修改类型影响过大，如需修改请联系管理员");
                                    }
                                },
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
